<template>
  <div class="main">
    <div class="topTitle">假期导入</div>
    <div class="form_content">
      <NSpin :show="spinShow">
        <div class="content">
          <NForm ref="formRef" label-placement="left" :label-width="130" label-align="right">
            <NFormItem>
              1.下载导入模板
              <NButton type="primary" @click="handleDownload" :loading="exportLoading"
                style="margin-left: 20rem; width: 115rem">
                下载
              </NButton>
            </NFormItem>
            <NFormItem>2.按照Excel格式,填充数据,不要调整列顺序</NFormItem>
            <NFormItem>3.选择要导入的文件</NFormItem>
            <NFormItem>
              <NUpload accept=".xlsx,.xls" @change="handleUploadChange" style="width: 200px" :max="1"
                :default-upload="false">
                <NButton type="primary">选择文件</NButton>
              </NUpload>
              <div style="
                  display: inline-block;
                  margin-left: -100rem;
                  width: 200rem;
                  color: rgb(104, 107, 109);
                " v-if="!fileListLengthRef">
                未选择任何文件
              </div>
            </NFormItem>
          </NForm>
        </div>
      </NSpin>
    </div>
  </div>
  <FixedAction>
    <NSpace justify="center">
      <NButton ghost @click="goBack">
        返回
      </NButton>
      <NButton type="primary" :disabled="!fileListLengthRef" @click="confirm" :loading="importLoading">
        导入
      </NButton>
    </NSpace>
  </FixedAction>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import {
  NSpace,
  NButton,
  useMessage,
  NUpload,
  NForm,
  NFormItem,
  NSpin,
  type UploadFileInfo
} from 'naive-ui'
import { useTagStore } from '@/stores/layout'
import FixedAction from '@/components/FixedAction.vue'
import { useRoute } from 'vue-router'
import router from '@/router'
import { sendRequest } from '@/utils/api'

const tags = useTagStore().tags
const route = useRoute()
onMounted(() => {
  console.log(route.query?.id)
})
const message = useMessage()

const spinShow = ref(false)

//下载
function download(headers: any, data: any, name?: string) {
  const val = headers['content-disposition']?.match(/(?<=filename=)(.*)/)
  let filename = val ? decodeURIComponent(val[0]) : name + '导出.xlsx'
  if (!filename.includes('xlsx')) {
    filename = filename.replace('xls', 'xlsx')
  }
  const a = document.createElement('a')
  let binaryData = []
  binaryData.push(data)
  a.href = window.URL.createObjectURL(new Blob(binaryData))
  a.download = filename
  a.click()
  window.URL.revokeObjectURL(a.href)
}
const exportLoading = ref(false)
async function handleDownload() {
  exportLoading.value = true
  const [error, res, headers] = await sendRequest({
    module: 'office',
    api: 'holidayDownLoad'
  })
  if (error) return message.error(error.message)
  download(headers, res)
  exportLoading.value = false
}

//导入
const importLoading = ref(false)
const fileListLengthRef: any = ref()
const fileDetail: any = ref()
async function handleUploadChange(data: { fileList: UploadFileInfo[]; file: any }) {
  fileListLengthRef.value = data.fileList.length
  fileDetail.value = data
}

async function handleClick(data: any) {
  spinShow.value = true
  importLoading.value = true
  const fileData = new FormData()
  fileData.append('file', data.file.file as File)
  const [error, res] = await sendRequest({
    module: 'office',
    api: 'holidayImport',
    override: {
      data: fileData
    }
  })
  spinShow.value = false
  importLoading.value = false
  if (error) return message.error(error.message)
  if (res.code != 0) return message.error(res.msg)
  message.success(res.data)
  await goBack()
}

// 返回
const goBack = () => {
  const index = tags.findIndex((item: any) => item.path === router.currentRoute.value.fullPath)

  tags.splice(index, 1)
  if (tags.length == 0) {
    router.push({ path: '/OA/Workbench/UserDesktop' })
  } else history.back()
}
// 确定
const confirm = async () => {
  await handleClick(fileDetail.value)
}

onMounted(() => { })
</script>

<style scoped lang="scss">
@mixin CommonFont {
  font-size: 16rem;
  font-family: PingFangSC, PingFang SC;
  font-weight: 400;
  color: rgba(0, 0, 0, 0.85);
}

.commonWidth {
  width: 360rem;
}

.main {
  display: flex;
  flex-direction: column;
  width: 100%;
  height: 100%;
  overflow: hidden;

  .topTitle {
    width: 100%;
    height: 64rem;
    border-bottom: 1px solid #fff;
    padding: 20rem;
    margin-bottom: 20rem;
    @include CommonFont;
  }

  .form_content {
    width: 100%;
    height: 100%;
    padding: 0 20rem 0rem;

    .content {
      display: flex;
      align-items: center;
      flex-direction: column;
      width: 100%;
      height: 100%;
      padding: 33rem 0 180rem;
      overflow-y: scroll;
      background-color: #ebeceb;

      .scope {
        width: 360rem;
        height: 130rem;
        background: #e1e1e1;
        margin-bottom: 25rem;
      }
    }
  }
}

.content::-webkit-scrollbar {
  display: none;
}

.scope {
  width: 360rem;
  height: 130rem;
  background: #e1e1e1;
  margin-bottom: 25rem;
}

.commonWidth {
  width: 360rem;
}

:deep(.n-spin-container) {
  width: 100%;
  height: 100%;
}

:deep(.n-scrollbar-container) {
  width: 100%;
  height: 100%;
}

:deep(.n-scrollbar-content) {
  width: 100%;
  height: 100%;
}

:deep(.n-spin-content) {
  width: 100%;
  height: 100%;
}
</style>
